/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * Class corresponding to the "viewdefss" element of views.xml.<br>
 * This contains all the views definitions.
 */
public class JBViewDefs implements Serializable
{
    /** Table of views. */
    private Map viewDefMap = new HashMap();

    /** Top level label. */
    private String label;

    /** Top level icon URI.*/
    private String icon;

//DW/2600/BeginPatch
    /** OrgChart parameters. */
    private JBOrgChartParams orgChartParams;
//DW/2600/EndPatch

    /** Creates new JBViewDefs. */
    public JBViewDefs()
    {
    }

    /**
     * Returns the list of views..
     * @return List of JBViewDef
     */
    public Map getViewDefList()
    {
        return viewDefMap;
    }

    /**
     * Adds a view definition to the list.
     *
     * @param v view to add
     */
    public void addViewDef(JBViewDef v)
    {
        this.viewDefMap.put(v.getName().trim().toLowerCase(), v);
    }

    /**
     * Gets a view from its name.
     * ASSERT : name should not be null
     *
     * @param name view name
     *
     * @return JBViewDef object
     */
    public JBViewDef findViewDefByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBViewDef) viewDefMap.get(key);
    }

    /**
     * Gets a list of views that a profile can see.
     *
     * @param profileName profile name
     *
     * @return list of views (JBViewDef)
     */
    public List findViewDefsByProfileName(String profileName)
    {
        List jbViewDefList = new ArrayList();

        Iterator it = viewDefMap.values().iterator();
        JBViewDef jbViewDef = null;

        while (it.hasNext())
        {
            jbViewDef = (JBViewDef) it.next();

            if ((jbViewDef.getJbViewDefProfiles() != null) &&
                    (jbViewDef.getJbViewDefProfiles().findViewDefProfileByName(profileName) != null))
            {
                jbViewDefList.add(jbViewDef);
            }
        }

        Object[] tmpList = jbViewDefList.toArray();

        Arrays.sort(tmpList);
        jbViewDefList = Arrays.asList(tmpList);

        return jbViewDefList;
    }

    /**
     * Displays the views definition.
     *
     * @return Views definition.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        Iterator it = null;

        if (viewDefMap != null)
        {
            buf.append("Map components of viewDefMap = ");
            buf.append(System.getProperty("line.separator"));
            it = viewDefMap.values().iterator();

            while (it.hasNext())
            {
                buf.append(".");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the top level icon URI.
     * @return icon URI
     */
    public String getIcon()
    {
        return icon;
    }

    /**
     * Returns the top level label.
     * @return label
     */
    public String getLabel()
    {
        return label;
    }

    /**
     * Sets the top level icon URI.
     * @param aicon The icon URI to set
     */
    public void setIcon(String aicon)
    {
        this.icon = aicon;
    }

    /**
     * Sets the top level label.
     * @param alabel The label to set
     */
    public void setLabel(String alabel)
    {
        this.label = alabel;
    }

//DW/2600/BeginPatch
    /**
     * Get orgchart parameters.
     * @return orgchart parameters.
     */
    public JBOrgChartParams getOrgChartParams()
    {
        return orgChartParams;
    }

    /**
     * Set orgchart parameters.
     * @param value orgchart parameters.
     */
    public void setOrgChartParams(JBOrgChartParams value)
    {
        this.orgChartParams = value;
    }
//DW/2600/EndPatch

}
